package com.example.demo.controller;

import com.example.demo.model.GymMembership;
import com.example.demo.repository.GymMembershipRepository;
import com.example.demo.service.GymMembershipService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.thymeleaf.util.StringUtils;

@Controller
public class GymMembershipController {
    @Autowired
    private GymMembershipRepository gymMembershipRepository;

    @Autowired
    private GymMembershipService gymMembershipService;

    @RequestMapping("gymMembership")
    public String getsGymMembership(ModelMap map, GymMembership gymMembership){
        if (StringUtils.isEmpty(gymMembership.getAge())){
            gymMembership.setAge(null);
        }
        if (StringUtils.isEmpty(gymMembership.getName())){
            gymMembership.setName(null);
        }
        map.put("GM", gymMembershipService.getAll(gymMembership));
        return "list";
    }
    @RequestMapping("gymMembership/delete/{id}")
    public String delete(@PathVariable Long id){
        gymMembershipService.delete(id);
        return "redirect:/gymMembership";
    }
    @RequestMapping("gymMembership/create")
    public String create(){
        return "create";
    }

    @RequestMapping("gymMembership/save")
    public String save(@RequestParam String name,@RequestParam String age){
        GymMembership gymMembership = new GymMembership();
        gymMembership.setAge(age);
        gymMembership.setName(name);
        gymMembershipService.save(gymMembership);
        return "redirect:/gymMembership";
    }
    @RequestMapping("gymMembership/update/{id}")
    public String update(ModelMap map,@PathVariable Long id){
        map.put("gymMembership", gymMembershipService.get(id));
        return "update";
    }

    @RequestMapping("gymMembership/edit")
    public String edit(GymMembership gymMembership){
        gymMembershipService.save(gymMembership);
        return "redirect:/gymMembership";
    }
}

